import decimal
import json
from django.http import HttpResponse

# 自定义一个JSON编码器类，处理JSON模块不能处理的特定类型的数据
class DecimalEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, decimal.Decimal):
            return float(obj)

#分类菜单
class MenuResponse():
    @staticmethod
    def success(data):
        result_json = {'status': '1000', 'data': []}
        for item in data:
            result_json['data'].append(item.to_dict())
        return HttpResponse(json.dumps(result_json), content_type='application/json')

    @staticmethod
    def failed(data):
        result_json = {'status': '1001', 'data': []}
        for item in data:
            result_json['data'].append(item.to_dict())
        return HttpResponse(json.dumps(result_json), content_type='application/json')

    @staticmethod
    def other(data):
        result_json = {'status': '1002', 'data': []}
        for item in data:
            result_json['data'].append(item.to_dict())
        return HttpResponse(json.dumps(result_json), content_type='application/json')

# 商品分类
class GoodsResponse():
    @staticmethod
    def success(data):
        result_json = {'status': '2000', 'data': []}
        for item in data:
            result_json['data'].append(item.to_dict())
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def failed(data):
        result_json = {'status': '2001', 'data': []}
        for item in data:
            result_json['data'].append(item)
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def other(data):
        result_json = {'status': '2002', 'data': data}
        # for item in data:
        #     result_json['data'].append(item)
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

# 购物车响应
class CartResponse():
    @staticmethod
    def success(data):
        result_json = {'status': '3000', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def failed(data):
        result_json = {'status': '3001', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def other(data):
        result_json = {'status': '3002', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')


# 用户响应
class UserResponse():
    @staticmethod
    def success(data):
        result_json = {'status': '4000', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def failed(data):
        result_json = {'status': '4001', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def other(data):
        result_json = {'status': '4002', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')


# 订单响应
class OrderResponse():
    @staticmethod
    def success(data):
        result_json = {'status': '5000', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def failed(data):
        result_json = {'status': '5001', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def other(data):
        result_json = {'status': '5002', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')



class GoodsListResponse():
    @staticmethod
    def success(data):
        result_json = {'status': '2000', 'data': []}
        for item in data:
            result_json['data'].append(item)
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def failed(data):
        result_json = {'status': '2001', 'data': []}
        for item in data:
            result_json['data'].append(item)
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def other(data):
        result_json = {'status': '2002', 'data': data}
        # for item in data:
        #     result_json['data'].append(item)
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

# 地址响应
class AddressResponse():
    @staticmethod
    def success(data):
        result_json = {'status': '7000', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def failed(data):
        result_json = {'status': '7001', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')

    @staticmethod
    def other(data):
        result_json = {'status': '7002', 'data': data}
        return HttpResponse(json.dumps(result_json, cls=DecimalEncoder), content_type='application/json')
